<?php

// +----------------------------------------------------------------------
// | 版权所有 2017-2027 云南环拓科技有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.yn123.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// | 注意：  这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用,
// |        不允许对程序代码以任何形式任何目的的再发布。
// +----------------------------------------------------------------------
// | Author: feraner <feraner@163.com> 
// | Date: 2019/9/29
// +----------------------------------------------------------------------

namespace app\index\controller\api;

use app\common\constants\ApiConstant;
use app\common\facade\Arr;
use think\App;
use think\Db;
use think\facade\Response;

/**
 *
 * Class ApiBase
 * @package app\index\controller\api
 * @author feraner<feraner@163.com>
 * @date 2019/9/29
 */
class ApiBase extends Base
{

    public function __construct(App $app = null)
    {
        parent::__construct($app);

        $this->__checkUser();

    }


    /**
     * 监测用户是否登录
     */
    private function __checkUser()
    {
        if (!$this->userId) {
            $this->response([], ApiConstant::USER_IS_NOT_LOGIN, ApiConstant::USER_IS_NOT_LOGIN_STR);
        }
    }



    /**
     * 前端 标准json数据格式
     * @param array  $data
     * @param int    $code
     * @param string $msg
     */
    public function response($data = [], $code = 200, $msg = '')
    {
        Response::create('', 'JSON')
            ->data([
                'data'  => $data,
                'code'  => $code,
                'msg'   => $msg,
                'time'  => time()
            ])
            ->send();
    }


    /**
     * 检查定位是否在系统中的开发地区，没在就取消这些定位条件搜索
     * @param $postData
     *
     * @return mixed
     */
    public function checkLocationInSystem($postData)
    {
        $inOpenArea = Db::name('system_agent_user')
            ->where('provinces_id', Arr::get($postData, 'provincesId', 0))
            ->where('city_id', Arr::get($postData, 'cityId', 0))
            ->where('pid', 0)
            ->where('status', 1)
            ->where('is_deleted', 0)
            ->value('id') ?: 0;
        if (!$inOpenArea) {
            unset($postData['provincesId']);
            unset($postData['cityId']);
            unset($postData['countyId']);
        }
        return $postData;
    }






}
